package com.iotimc.attendancesystem.attendance;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.iotimc.attendancesystem.event.TimePromptEvent;
import com.iotimc.attendancesystem.util.TtsUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

/**
 * Created by cwj on 2017/12/8.
 */

public class TimePromptService extends Service {
    private static final String TAG = "TimePromptService";

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter timeFilter = new IntentFilter();
        timeFilter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(mTimeReceiver, timeFilter);
        Log.i(TAG, "registerReceiver ");


        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.setPriority(Integer.MAX_VALUE);
        registerReceiver(mEthernetReceiver, filter);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (mTimeReceiver != null) {
            unregisterReceiver(mTimeReceiver);
        }
        super.onDestroy();

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private BroadcastReceiver mTimeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Calendar cal = Calendar.getInstance();
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int min = cal.get(Calendar.MINUTE);
            if (min == 0) {
                TtsUtil.ttsSpeek2("现在是北京时间" + hour + "点整");
                EventBus.getDefault().post(new TimePromptEvent(true));
            } else if (min == 30) {
                TtsUtil.ttsSpeek2("现在是北京时间" + hour + "点三十分");
            }
        }
    };



    private BroadcastReceiver mEthernetReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //判断网络状态
            ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (conn != null) {
                NetworkInfo[] networkInfo = conn.getAllNetworkInfo();
                if (networkInfo != null) {
                    for (NetworkInfo aNetworkInfo : networkInfo) {

                        if (aNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                            //有线网络连接成功
                            if ("CONNECTED".equals(aNetworkInfo.getState().name())) {
                                //有线网络连接成功
                                EventBus.getDefault().post(new TimePromptEvent(true));
                            }
                        }else if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            //WIFI网络连接成功
                            if ("CONNECTED".equals(aNetworkInfo.getState().name())) {
                                //WIFI连接成功
                                EventBus.getDefault().post(new TimePromptEvent(true));
                            }
                        }
                    }
                }
            }

        }
    };

}
